<?php

namespace app\ruinuo\config\app;
use app\ruinuo\config\app\Logic as OkLogic;
use app\common\service\Excel as ExcelService;
use app\common\service\Lists as ListsService;
use app\common\service\Power as PowerService;
use app\ruinuo\config\app\Info as ThatInfo;

class Lists
{
    public function Mode()
    {
        $ThatInfo = new ThatInfo();
        return $ThatInfo->ListModel();
    }

    public function GetLists()
    {
        $ThatInfo = new ThatInfo();
        return $ThatInfo->InfoLists();
    }

    public function GetSetting($field)
    {
        $ThatInfo = new ThatInfo();
        $InfoMode = $ThatInfo->InfoMode();
        return $InfoMode[$field];
    }

    public function SetWhere($code = '', $params = [])
    {
        $Lists = $this->GetLists();
        $ListsService = new ListsService();
        return $ListsService->SetWhere($code, $Lists);
    }

    public function QueryWhere($code = '', $params = [])
    {
        $Lists = $this->GetLists();
        $ListsService = new ListsService();
        return $ListsService->QueryWhere($code, $Lists, $params);

    }

    public function Field($code = '', $params = [], $type = 1)
    {
        $Lists = $this->GetLists();
        $ListsService = new ListsService();
        return $ListsService->ListField($code, $Lists,$type);
    }

    public function Raw($code = '', $params = [])
    {
        $Lists = $this->GetLists();
        $ListsService = new ListsService();
        return $ListsService->ListRaw($code, $Lists, $params);
    }

    public function GetSource($code = '')
    {
        $Lists = $this->GetLists();
        $ListsService = new ListsService();
        return $ListsService->ListSource($code, $Lists);
    }

    public function Tabs($params = [])
    {
        $ThatInfo = new ThatInfo();
        $PowerService = new PowerService();
        $ListsService = new ListsService();
        $Lists = $this->GetLists();
        $code = !empty($params['menu_code']) ? $params['menu_code'] : $Lists[0]['num'];
        $data = [
            'menu' => $ListsService->ListMenu($Lists),
            'total' => $this->GetTotal($code, $params),
            'source' => $this->GetSource($code),
            'table_button' => $PowerService->Button($this->GetSetting('mode_id'), 2),
            'head_button' => $PowerService->Button($this->GetSetting('mode_id'), 1),
        ];
        foreach ($data['menu'] as $k => $v) {
            if ($code == $v['code']) {
                $data['menu'][$k]['on'] = true;
            }
            $data['menu'][$k]['num'] = $this->Count($v['code']);
        }
        return $data;
    }

    public function GetTotal($code = '', $params = [])
    {
        $ListsService = new ListsService();
        $Lists = $this->GetLists();
        $data = $ListsService->ListTotal($code, $Lists);
        foreach ($data as $k => $v) {
            $data[$k]['num'] = $this->Total($code, $v['code'], $params);
        }
        return $data;
    }

    public function Total($code = '', $field = '', $params = [])
    {
        $count = $this->Mode()
            ->where($this->SetWhere($code))
            ->where($this->QueryWhere($code, $params))
            ->sum($field);
        return $count;
    }

    public function Count($params = [])
    {
        $Lists = $this->GetLists();
        $code = !empty($params['menu_code']) ? $params['menu_code'] : $Lists[0]['num'];
        $count = $this->Mode()
            ->where($this->SetWhere($code))
            ->where($this->QueryWhere($code, $params))
            ->count();
        return $count;
    }

    public function Lists($params = [])
    {
        $page = !empty($params['page']) ? $params['page'] - 1 : 0;
        $limit = !empty($params['limit']) ? $params['limit'] : 1000000;
        $Lists = $this->GetLists();
        $code = !empty($params['menu_code']) ? $params['menu_code'] : $Lists[0]['num'];
        $rows= $this->Mode()
            ->field('a.id,' . $this->Field($code, $params))
            ->where($this->SetWhere($code, $params))
            ->where($this->QueryWhere($code, $params))
            ->orderRaw($this->Raw($code, $params))
            ->limit($page * $limit, $limit)
            ->select()
            ->toArray();
        $ListsService = new ListsService();
        $rows=$ListsService->ListData($rows);
        return $rows;
    }

    public function InExcel($file = '')
    {
        if ($file) {
            $ExcelService = new ExcelService();
            return $ExcelService->Import($file);
        }
        $arr = input("data");
        $OkLogic = new OkLogic();
        $data = [];
        foreach ($arr as $k => $v) {
            $item = $OkLogic->Add($v);
            $v['msg'] = $item['msg'];
            $v['is_fail'] = 2;
            if ($item['code'] != 200) {
                $v['is_fail'] = 1;
            }
            $data[] = $v;
        }
        return ['code' => 200, 'msg' => '', 'data' => $data];
    }

    public function OutExcel($params = [])
    {
        $Lists = $this->GetLists();
        $code = !empty($params['menu_code']) ? $params['menu_code'] : $Lists[0]['num'];
        $lists = $this->Mode()
            ->field($this->Field($code, $params))
            ->where($this->SetWhere($code, $params))
            ->where($this->QueryWhere($code, $params))
            ->orderRaw($this->Raw($code, $params))
            ->select()
            ->toArray();
        $ExcelService = new ExcelService();
        $name = $this->GetSetting('mode_name');
        $data = [[
            'title' => $name,
            'cols' => $this->Field($code, $params, 2),
            'list' => $lists
        ]];
        $ExcelService->Out($data, $name);
    }

    public function TemplateExcel()
    {
        $ListsService = new ListsService();
        $ThatInfo = new ThatInfo();
        $InfoForm=$ThatInfo->InfoForm();
        $field = $ListsService->ExcelField($InfoForm);
        $name = $this->GetSetting('mode_name');
        $ExcelService = new ExcelService();
        $ExcelService->Template($name, $field);
    }




}